package com.movie.admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.movie.datacomm.common.CommResp;
import com.movie.datacomm.model.Cinema;
import com.movie.datacomm.service.CinemaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.lang.Nullable;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/cinema")
public class CinemaController {

    @Autowired
    CinemaService cinemaService;


    @RequestMapping(value = "/getCurrentPageCinema", method = RequestMethod.GET)
    @ResponseBody
    public CommResp getCurrentPageCinema(int currentPage, int pageSize, @Nullable String input){

        Page<Cinema> page = new Page<>(currentPage, pageSize);

        QueryWrapper<Cinema> wrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(input)){
            wrapper.like("cinema_name", input);
        }
        wrapper.orderByAsc("cinema_id");

        page = cinemaService.page(page, wrapper);
        return CommResp.okList(page.getRecords(), page.getTotal());
    }

    @RequestMapping(value = "/updateCinemaInfo", method = RequestMethod.POST)
    @ResponseBody
    public CommResp updateCinemaInfo(@RequestBody Cinema cinema){
        Cinema tmpCinema = cinemaService.getOne(new QueryWrapper<Cinema>().eq("cinema_name", cinema.getCinemaName()).le(false, "cinema_id", cinema.getCinemaId()));
        if(null != tmpCinema){
            return CommResp.fail("影院名已存在！");
        }

        boolean result = cinemaService.updateById(cinema);
        if(result) {
            return CommResp.ok();
        }else{
            return CommResp.fail("更新失败");
        }
    }

    @RequestMapping(value = "/addCinemaInfo", method = RequestMethod.POST)
    @ResponseBody
    public CommResp addCinemaInfo(@RequestBody Cinema cinema){
        Cinema tmpCinema = cinemaService.getOne(new QueryWrapper<Cinema>().eq("cinema_name", cinema.getCinemaName()));
        if(null != tmpCinema){
            return CommResp.fail("影院名已存在！");
        }

        boolean result = cinemaService.save(cinema);
        if(result) {
            return CommResp.ok();
        }else{
            return CommResp.fail("操作失败");
        }
    }

    @RequestMapping(value = "/deleteCinemaInfo", method = RequestMethod.POST)
    @ResponseBody
    public CommResp deleteCinemaInfo(@RequestBody Cinema cinema){

        boolean result = cinemaService.removeById(cinema.getCinemaId());
        if(result) {
            return CommResp.ok();
        }else{
            return CommResp.fail("删除失败");
        }
    }

}
